<?php

/**
 * Created by Phpstorm.
 * User: netman
 * Date: 15-9-23
 * Time: 上午10:43
 * Desc: 资讯标签
 */
class Taglib_News
{

    private static $_typeid = 115;
    private static $basefield = 'a.id,
                                a.webid,
                                a.aid,
                                a.title,
                                a.seotitle,
                                a.comefrom,
                                a.fromsite,
                                a.author,
                                a.litpic,
                                a.addtime,
                                a.modtime,
                                a.shownum,
                                a.tagword,
                                a.keyword,
                                a.description,
                                a.ishidden,
                                a.templet,
                                a.summary,
                                a.piclist,
                                a.attachment,
                                a.category_one,
                                a.category_two
                                ';

    /**
     * 检测该产品模块是否安装
     * @return bool
     */
    public function right()
    {
        $bool = true;
        if (!St_Functions::is_system_app_install(self::$_typeid))
        {
            $bool = false;
        }
        return $bool;
    }


    /**
     * @param $params
     * @return mixed
     * @description 标签接口
     */
    public static function query($params)
    {
        $default = array(
            'flag' => '',
            'destid' => 0,
            'offset' => 0,
            'row' => '10',
            'havepic' => false,
            'tagword' => '',
        );
        $params = array_merge($default, $params);
        extract($params);
        switch ($flag)
        {
            case 'new':
                $list = self::get_article_new($offset, $row, $havepic);
                break;
            case 'order':
                $list = self::get_article_order($offset, $row, $havepic);
                break;
            case 'relative':
                if (empty($categoryid)) return '';
                $list = self::get_article_by_attrid($offset, $row, $categoryid);
                break;
            case 'hot':
                $list = self::get_article_hot($offset, $row, $havepic);
                break;
            case 'category':
                $list = self::get_article_by_attrid($offset, $row, $categoryid);
                break;
            case 'tagrelative':
                $list = self::get_article_by_tagword($row, $offset, $tagword, $havepic);
                break;

        }
        foreach($list as &$a)
        {
            $a['articleid'] = $a['id'];

        }
        return self::get_article_attachinfo($list);

    }

    /**
     * @function 资讯分类获取
     * @param $params
     */
    public static function category($params)
    {
        $default = array(
            'flag' => '',
            'offset' => 0,
            'row' => '10',
            'pid' => 0,
        );
        $params = array_merge($default, $params);
        extract($params);
        switch ($flag)
        {
            case 'top':
                $pid = 0;
                $list = self::get_category_list($offset, $row, $pid);
                break;
            case 'next':

                $list = self::get_category_list($offset, $row, $pid);
                break;


        }
        return $list;

    }


    public static function get_article_attachinfo($list)
    {

        //对获取的数据进行初始化处理
        foreach($list as &$v)
        {
            if(ISMOBILE==1)
            {
                $v['url'] = Common::get_web_url(0) . "/news/{$v['aid']}.html";
            }
            else
            {
                $v['url'] = Common::get_web_url($v['webid']) . "/news/{$v['aid']}.html";
            }
        }
        unset($v);
        return $list;
    }

    /*
   * 获取最新
   * */
    public static function get_article_new($offset,$row,$havepic)
    {
        $sql = "SELECT ".self::$basefield." FROM sline_news a ";
        $sql.= "WHERE a.ishidden=0 ";
        if($havepic)
            $sql.= "and a.litpic<>'' ";
        $sql.= "ORDER BY a.modtime desc,a.addtime DESC ";
        $sql.= "LIMIT {$offset},{$row}";
        $arr = self::execute($sql);
        return $arr;
    }
    /*
     * 按顺序获取
     * */
    public static function get_article_order($offset,$row,$havepic)
    {
        $sql = "SELECT ".self::$basefield." FROM sline_news a ";
        $sql.= "LEFT JOIN `sline_allorderlist` b ";
        $sql.= "ON (a.id=b.aid and b.typeid=".self::$_typeid.") ";
        $sql.= "WHERE a.ishidden=0 ";
        if($havepic)
            $sql.= "and a.litpic<>'' ";
        $sql.= "ORDER BY IFNULL(b.displayorder,9999) asc,a.modtime desc,a.addtime DESC ";
        $sql.= "limit {$offset},{$row}";
        $arr = self::execute($sql);
        return $arr;


    }
    /*
  * 获取热门
  * */
    public static function get_article_hot($offset,$row,$havepic)
    {
        $sql = "SELECT ".self::$basefield." FROM sline_news a ";
        $sql.= "WHERE a.ishidden=0 ";
        if($havepic)
            $sql.= "and a.litpic<>'' ";
        $sql.= "ORDER BY a.shownum desc ";
        $sql.= "LIMIT {$offset},{$row}";
        $arr = self::execute($sql);
        return $arr;
    }



    /*
  * 执行sql语句
  * */
    private static function execute($sql)
    {
        $arr = DB::query(1,$sql)->execute()->as_array();
        return $arr;
    }


    /**
     * @param $offset
     * @param $row
     * @param $attrid
     * @return mixed
     * @desc 按属性读取文章
     */
    private static function get_article_by_attrid($offset, $row, $attrid)
    {
        $sql = "SELECT a.* FROM sline_news a ";
        $sql .= "LEFT JOIN `sline_allorderlist` b ";
        $sql .= "ON (a.id=b.aid and b.typeid=".self::$_typeid.") ";
        $sql .= "WHERE a.ishidden=0  AND (category_one='$attrid' OR category_two='$attrid')";

        $sql .= "ORDER BY IFNULL(b.displayorder,9999) asc,a.modtime desc,a.addtime DESC ";
        $sql .= "limit {$offset},{$row}";
        $arr = DB::query(1, $sql)->execute()->as_array();
        return $arr;

    }

    public static function get_article_by_tagword($row, $offset, $tagword, $havepic)
    {
        $offset = intval($offset);
        $row = intval($row);
        $tagword_arr = explode(",", $tagword);
        if (count($tagword_arr) <= 0)
            return array();

        $sql = "SELECT " . self::$basefield . " FROM sline_news a ";
        $sql .= "LEFT JOIN `sline_allorderlist` b ";
        $sql .= "ON (a.id=b.aid and b.typeid=" . self::$_typeid . ") ";
        $sql .= "WHERE a.ishidden=0 AND ( ";
        foreach ($tagword_arr as $tagword_item)
        {
            $sql .= "FIND_IN_SET('{$tagword_item}',a.tagword) OR ";
        }
        $sql = rtrim($sql, " OR ");
        $sql .= ") ";
        if($havepic)
            $sql.= "and a.litpic<>'' ";
        $sql .= "ORDER BY IFNULL(b.displayorder,9999) asc,a.modtime desc,a.addtime DESC ";
        $sql .= "LIMIT {$offset},{$row}";
        $arr = DB::query(1, $sql)->execute()->as_array();
        return $arr;
    }

    /**
     * @function 获取分类列表
     * @param $pid
     * @return mixed
     */
    private static function get_category_list($offset, $row, $pid)
    {
        $out = DB::select()->from('news_attr')->where('pid','=',$pid)
            ->and_where('isopen','=',1)->order_by('displayorder', 'ASC')->execute()->as_array();

        foreach($out as &$v)
        {
            $v['title'] = $v['attrname'];
        }
        return $out;
    }




} 